package com.file.homework;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

/**
 * @author げっしょく
 * @date 2021/8/2
 */
public class Demo7 {
    /*
     键盘录入一个任意字符串，统计字符串中相邻字符出现的次数，将结果写到D盘下的count.txt文件中（面试题）
    例如：aaccaabaab
    count.txt：2a2c2a1b2a1b
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("D:/count.txt");
            char[] c = s.toCharArray();
            for (int i = 0; i < c.length; i++) {
                char a = c[i];
                int count = 1;
                if(i != c.length - 1 ){
                    i++;
                    for ( ;c[i] == a && i < c.length ; i++) {
                        count ++;
                    }
                    fos.write(count + 48 );
                    fos.write( a );
                    i--;
                }
                }

            fos.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
